package com.sbwebsocket.core.redis;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sbwebsocket.core.model.bo.UnusualMessage;
import com.sbwebsocket.core.redis.model.contants.WsMessageRedisKeyPrefix;
import com.sbwebsocket.core.service.OfflineMessageManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;

public class RedisOfflineMessageManager implements OfflineMessageManager {
    
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private ObjectMapper objectMapper;
    

    @Override
    public boolean save(UnusualMessage message) {
        Long size = redisTemplate.opsForList().leftPush(getKey(message.getClientKey()), message.getJsonBody());
        return size>0;
    }

    @Override
    public List<String> find(String clientIdentity) {
        List<String> messages = redisTemplate.opsForList().range(getKey(clientIdentity), 0, -1);
        return messages;
    }
    
    private String getKey(String clientIdentity){
        return WsMessageRedisKeyPrefix.OFFLINE_MSG + clientIdentity;
    }
}
